<?php

namespace App\Http\Controllers\Admin;

use App\Models\FangOwner;
use Illuminate\Http\Request;
use App\Http\Controllers\Admin\BaseController;

use App\Exports\FangOwnerExport;
use Maatwebsite\Excel\Facades\Excel;

class FangOwnerController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $data = FangOwner::paginate($this->item_num_per_page);

        return view('admin.fangowner.index', compact('data'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('admin.fangowner.create');
    }

    //文件上传
    public function upfile(Request $request) {
        $pic = config('up.pic');
        if ($request->hasFile('file')) {
            //上传身份证到cfg_fangowner_pic
            $new_file_name = $request->file('file')->store('', 'cfg_fangowner_pic');
            if (is_string($new_file_name)) {
                //保存成功
                $pic = '/uploads/fangowner/' . $new_file_name; //完整路径
            }
        }
        return ['status' => 0, 'url' => $pic];
    }

    //删除已上传的房东身份证照片
    public function delfile(Request $request) {
        $filepath = $request->get('file');
        //得到真实的地址,拼接，并删除图片文件
        $path = public_path().$filepath;
        //$path = "E:\main\work\php\laravel\zfw\public/uploads/fangowner/bTXHzm3WXY0j8F9pTv1hesuFlza8SwC8bZiggY4m.jpeg"
        unlink($path);

        return ['status' => 0, 'msg' => '成功'];
    }

    /**
     * 添加房东 处理
     */
    public function store(Request $request)
    {
        $this->validate($request, [
            'name' => 'required',
            'phone' => 'required',
        ]);

        $postData = $request->except(['_token', 'file']);
        //去除#号,trim默认不写去空格，写了就去除写的
        $postData['pic'] = trim($postData['pic'], '#');
        //入库
        FangOwner::create($postData);
        //跳转
        return redirect(route('admin.fangowner.index'));
    }

    //显示照片 展示到layer_show弹出层
    public function show(FangOwner $fangowner)
    {
        //偷懒，按道理应该弄个blade返回出去
        $picList = explode('#', $fangowner->pic);
        $html = '';
        //遍历
        array_map(function($item) use($html){
            echo "<div><img src=$item style='width: 100px'/></div>";
        }, $picList);

        return '';
    }

    public function exports() {
        return Excel::download(new FangOwnerExport(), 'fangdong.xlsx');
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \App\Models\FangOwner  $fangOwner
     * @return \Illuminate\Http\Response
     */
    public function edit(FangOwner $fangowner)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\FangOwner  $fangOwner
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, FangOwner $fangowner)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\FangOwner  $fangOwner
     * @return \Illuminate\Http\Response
     */
    public function destroy(FangOwner $fangowner)
    {
        //
    }
}
